//
//  Created by Jesse Squires
//  http://www.jessesquires.com
//
//
//  Documentation
//  http://cocoadocs.org/docsets/JSQMessagesViewController
//
//
//  GitHub
//  https://github.com/jessesquires/JSQMessagesViewController
//
//
//  License
//  Copyright (c) 2014 Jesse Squires
//  Released under an MIT license: http://opensource.org/licenses/MIT
//

#import "JSQMessagesToolbarContentView.h"

#import "UIView+JSQMessages.h"

const CGFloat kJSQMessagesToolbarContentViewHorizontalSpacingDefault = 32.0f;


@interface JSQMessagesToolbarContentView () <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet JSQMessagesComposerTextView *textView;

@property (weak, nonatomic) IBOutlet UIView *leftBarButtonContainerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftBarButtonContainerViewWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftHorizontalSpacingConstraint;

@property (weak, nonatomic) IBOutlet UIView *rightOtherBarButtonContainerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightOtherBarButtonContainerViewWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightOtherHorizontalSpacingConstraint;

@property (weak, nonatomic) IBOutlet UIView *rightFaceBarButtonControllerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightFaceButtonControllerWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightFaceHorizontalSpacingConstraint;

@end



@implementation JSQMessagesToolbarContentView

#pragma mark - Class methods

+ (UINib *)nib {
    return [UINib nibWithNibName:NSStringFromClass([JSQMessagesToolbarContentView class])
                          bundle:[NSBundle bundleForClass:[JSQMessagesToolbarContentView class]]];
}

#pragma mark - Initialization

- (void)awakeFromNib {
    [super awakeFromNib];

    [self setTranslatesAutoresizingMaskIntoConstraints:NO];

    self.leftHorizontalSpacingConstraint.constant = kJSQMessagesToolbarContentViewHorizontalSpacingDefault;
    self.rightOtherHorizontalSpacingConstraint.constant = kJSQMessagesToolbarContentViewHorizontalSpacingDefault;
    self.rightFaceHorizontalSpacingConstraint.constant = kJSQMessagesToolbarContentViewHorizontalSpacingDefault;

    self.backgroundColor = [UIColor clearColor];
}

- (void)dealloc {
    _textView = nil;
    _leftBarButtonItem = nil;
    _rightOtherBarButtonItem = nil;
    _leftBarButtonContainerView = nil;
    _rightOtherBarButtonContainerView = nil;
    _rightFaceBarButtonControllerView = nil;
}

#pragma mark - Setters

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    self.leftBarButtonContainerView.backgroundColor = backgroundColor;
    self.rightOtherBarButtonContainerView.backgroundColor = backgroundColor;
    self.rightFaceBarButtonControllerView.backgroundColor = backgroundColor;
}

- (void)setLeftBarButtonItem:(UIButton *)leftBarButtonItem {
    if (_leftBarButtonItem) {
        [_leftBarButtonItem removeFromSuperview];
    }

    if (!leftBarButtonItem) {
        _leftBarButtonItem = nil;
        self.leftHorizontalSpacingConstraint.constant = 0.0f;
        self.leftBarButtonItemWidth = 0.0f;
        self.leftBarButtonContainerView.hidden = YES;
        return;
    }

    if (CGRectEqualToRect(leftBarButtonItem.frame, CGRectZero)) {
        leftBarButtonItem.frame = self.leftBarButtonContainerView.bounds;
    }

    self.leftBarButtonContainerView.hidden = NO;
    self.leftHorizontalSpacingConstraint.constant = kJSQMessagesToolbarContentViewHorizontalSpacingDefault;
    self.leftBarButtonItemWidth = CGRectGetWidth(leftBarButtonItem.frame);

    [leftBarButtonItem setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.leftBarButtonContainerView addSubview:leftBarButtonItem];
    [self.leftBarButtonContainerView jsq_pinAllEdgesOfSubview:leftBarButtonItem];
    [self setNeedsUpdateConstraints];

    _leftBarButtonItem = leftBarButtonItem;
}

- (void)setLeftBarButtonItemWidth:(CGFloat)leftBarButtonItemWidth {
    self.leftBarButtonContainerViewWidthConstraint.constant = leftBarButtonItemWidth;
    [self setNeedsUpdateConstraints];
}

- (void)setRightOtherBarButtonItem:(UIButton *)rightOtherBarButtonItem {
    if (_rightOtherBarButtonItem) {
        [_rightOtherBarButtonItem removeFromSuperview];
    }

    if (!rightOtherBarButtonItem) {
        _rightOtherBarButtonItem = nil;
        self.rightOtherHorizontalSpacingConstraint.constant = 0.0f;
        self.rightOtherBarButtonItemWidth = 0.0f;
        self.rightOtherBarButtonContainerView.hidden = YES;
        return;
    }

    if (CGRectEqualToRect(rightOtherBarButtonItem.frame, CGRectZero)) {
        rightOtherBarButtonItem.frame = self.rightOtherBarButtonContainerView.bounds;
    }

    self.rightOtherBarButtonContainerView.hidden = NO;
    self.rightOtherHorizontalSpacingConstraint.constant = kJSQMessagesToolbarContentViewHorizontalSpacingDefault;
    self.rightOtherBarButtonItemWidth = CGRectGetWidth(rightOtherBarButtonItem.frame);

    [rightOtherBarButtonItem setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.rightOtherBarButtonContainerView addSubview:rightOtherBarButtonItem];
    [self.rightOtherBarButtonContainerView jsq_pinAllEdgesOfSubview:rightOtherBarButtonItem];
    [self setNeedsUpdateConstraints];

    _rightOtherBarButtonItem = rightOtherBarButtonItem;
}

- (void)setRightFaceBarButtonItem:(UIButton *)rightFaceBarButtonItem {
    if (_rightFaceBarButtonItem) {
        [_rightFaceBarButtonItem removeFromSuperview];
    }

    if (!rightFaceBarButtonItem) {
        _rightFaceBarButtonItem = nil;
        self.rightFaceHorizontalSpacingConstraint.constant = 0.0f;
        self.rightFaceBarButtonItemWidth = 0.0f;
        self.rightFaceBarButtonControllerView.hidden = YES;
        return;
    }

    if (CGRectEqualToRect(rightFaceBarButtonItem.frame, CGRectZero)) {
        rightFaceBarButtonItem.frame = self.rightFaceBarButtonControllerView.bounds;
    }

    self.rightFaceBarButtonControllerView.hidden = NO;
    self.rightFaceHorizontalSpacingConstraint.constant = kJSQMessagesToolbarContentViewHorizontalSpacingDefault;
    self.rightFaceBarButtonItemWidth = CGRectGetWidth(rightFaceBarButtonItem.frame);

    [rightFaceBarButtonItem setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.rightFaceBarButtonControllerView addSubview:rightFaceBarButtonItem];
    [self.rightFaceBarButtonControllerView jsq_pinAllEdgesOfSubview:rightFaceBarButtonItem];
    [self setNeedsUpdateConstraints];

    _rightFaceBarButtonItem = rightFaceBarButtonItem;
}

- (void)setRightOtherBarButtonItemWidth:(CGFloat)rightOtherBarButtonItemWidth {
    self.rightOtherBarButtonContainerViewWidthConstraint.constant = rightOtherBarButtonItemWidth;
    [self setNeedsUpdateConstraints];
}

- (void)setRightOtherContentPadding:(CGFloat)rightOtherContentPadding {
    self.rightOtherHorizontalSpacingConstraint.constant = rightOtherContentPadding;
    [self setNeedsUpdateConstraints];
}

- (void)setRightFaceContentPadding:(CGFloat)rightFaceContentPadding {
    self.rightFaceHorizontalSpacingConstraint.constant = rightFaceContentPadding;
    [self setNeedsUpdateConstraints];
}

- (void)setLeftContentPadding:(CGFloat)leftContentPadding {
    self.leftHorizontalSpacingConstraint.constant = leftContentPadding;
    [self setNeedsUpdateConstraints];
}

#pragma mark - Getters

- (CGFloat)leftBarButtonItemWidth {
    return self.leftBarButtonContainerViewWidthConstraint.constant;
}

- (CGFloat)rightOtherBarButtonItemWidth {
    return self.rightOtherBarButtonContainerViewWidthConstraint.constant;
}

- (CGFloat)rightOtherContentPadding {
    return self.rightOtherHorizontalSpacingConstraint.constant;
}

- (CGFloat)rightFaceContentPadding {
    return self.rightFaceHorizontalSpacingConstraint.constant;
}

- (CGFloat)rightFaceBarButtonItemWidth {
    return self.rightFaceButtonControllerWidthConstraint.constant;
}

- (CGFloat)leftContentPadding {
    return self.leftHorizontalSpacingConstraint.constant;
}

#pragma mark - UIView overrides

- (void)setNeedsDisplay {
    [super setNeedsDisplay];
    [self.textView setNeedsDisplay];
}

@end
